using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NET.Library.Web.Forms.DataControls.DataGrids
{
    /// <summary>
    /// Populate variable types with values from DataGrid control data items.
    /// </summary>
    public static class DataGridDataItems
    {
        /// <summary>
        /// Return DataGrid control data item as a string type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string StringDataItem(string fieldName, DataGridItemEventArgs e)
        {
            return DataItem(fieldName, e).ToString();
        }

        /// <summary>
        /// Return DataGrid control data item as a DateTime type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static DateTime DateTimeDataItem(string fieldName, DataGridItemEventArgs e)
        {
            return (DateTime)DataItem(fieldName, e);
        }

        /// <summary>
        /// Return DataGrid control data item as an integer type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static int IntDataItem(string fieldName, DataGridItemEventArgs e)
        {
            return (int)DataItem(fieldName, e);
        }

        /// <summary>
        /// Return DataGrid control data item used as a Boolean type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static bool BoolDataItem(string fieldName, DataGridItemEventArgs e)
        {
            //return (bool)DataItem(fieldName, e);
            object Object = DataItem(fieldName, e);
            return Object == DBNull.Value ? false : Convert.ToBoolean(Object);
        }

        #region Private Methods
        private static object DataItem(string fieldName, DataGridItemEventArgs e)
        {
            return DataBinder.Eval(e.Item.DataItem, fieldName);
        }
        #endregion
    }
}
